using Infrastructure.Attribute;
using Infrastructure.Extensions;
using JTZY.Model.Business.Dto;
using JTZY.Model.Business;
using JTZY.Repository;
using JTZY.Service.Business.IBusinessService;
using JTZY.Model.Goods.Dto;
using JTZY.Model.Product;
using Aliyun.OSS;
using JTZY.Model.Order;
using JTZY.Model.Agent;

namespace JTZY.Service.Business
{
    /// <summary>
    /// 商品浏览记录表Service业务层处理
    /// </summary>
    [AppService(ServiceType = typeof(ICorecmsgoodsbrowsingService), ServiceLifetime = LifeTime.Transient)]
    public class CorecmsgoodsbrowsingService : BaseService<Corecmsgoodsbrowsing>, ICorecmsgoodsbrowsingService
    {
        /// <summary>
        /// 查询商品浏览记录表列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public PagedInfo<CorecmsgoodsbrowsingDto> GetList(CorecmsgoodsbrowsingQueryDto parm)
        {
            var predicate = QueryExp(parm);

            var response = Queryable().LeftJoin<Corecmsuser>((it,user)=>it.UserId==user.Id)
                .Where((it,user)=>it.GoodUserId==parm.userId)
                .WhereIF(!string.IsNullOrEmpty(parm.areas),(it,user)=>user.AreaIds.StartsWith(parm.areas))
                .WhereIF(!string.IsNullOrEmpty(parm.userName), (it, user) => user.UserName.StartsWith(parm.userName))
                .Where((it, user) => it.CreateTime >= parm.dateTime && it.CreateTime < parm.dateTime.AddDays(1))
                .GroupBy((it, user) => new { it.UserId,user.UserName }).Select((it, user) =>
                    new Corecmsgoodsbrowsing { 
                        UserId = it.UserId,
                        UserName = user.UserName,
                        CreateTime = SqlFunc.AggregateMin(it.CreateTime),
                    }
                )
                .ToPage<Corecmsgoodsbrowsing, CorecmsgoodsbrowsingDto>(parm);

            return response;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public UserVisitReportDto GetUserVisit(CorecmsgoodsbrowsingQueryDto parm)
        {
            UserVisitReportDto userVisitDto = new UserVisitReportDto();
            userVisitDto.VisitCount = Queryable().Where(s => s.GoodUserId == parm.userId && s.CreateTime >= parm.dateTime && s.CreateTime < parm.dateTime.AddDays(1)).GroupBy(s=>s.GoodUserId).Count();
            userVisitDto.newUserCount = Context.Queryable<Corecmsuser>().Where(s => s.ParentId == parm.userId && s.CreateTime >= parm.dateTime && s.CreateTime < parm.dateTime.AddDays(1)).Count();
            userVisitDto.user = Context.Queryable<Corecmsuser>().Where(it => it.Id == parm.userId).First();
            userVisitDto.UserCount = Context.Queryable<Corecmsuser>().Where(it => it.ParentId == parm.userId).Count();
            userVisitDto.UnReUserCount = Context.Queryable<Corecmsuser>().Where(it => it.ParentId == parm.userId&&it.IsReviewed!=1).Count();
            userVisitDto.ReUserCount = Context.Queryable<Corecmsuser>().Where(it => it.ParentId == parm.userId && it.IsReviewed == 1).Count();
            userVisitDto.OrderCount= Context.Queryable<Corecmsagentorder>().Where(s => s.UserId == parm.userId && s.CreateTime >= parm.dateTime && s.CreateTime < parm.dateTime.AddDays(1)).Count();
            return userVisitDto;
        }


        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public Corecmsgoodsbrowsing GetInfo(int Id)
        {
            var response = Queryable()
                .Where(x => x.Id == Id)
                .First();

            return response;
        }

        /// <summary>
        /// 添加商品浏览记录表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public Corecmsgoodsbrowsing AddCorecmsgoodsbrowsing(Corecmsgoodsbrowsing model)
        {
            return Insertable(model).ExecuteReturnEntity();
        }

        /// <summary>
        /// 修改商品浏览记录表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UpdateCorecmsgoodsbrowsing(Corecmsgoodsbrowsing model)
        {
            return Update(model, true);
        }

        /// <summary>
        /// 查询导出表达式
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        private static Expressionable<Corecmsgoodsbrowsing> QueryExp(CorecmsgoodsbrowsingQueryDto parm)
        {
            var predicate = Expressionable.Create<Corecmsgoodsbrowsing>();
            predicate.And(it => it.CreateTime >= parm.dateTime && it.CreateTime < parm.dateTime.AddDays(1));
            return predicate;
        }
    }
}